package com.tianitan.gen.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianitan.gen.mapper.UserMapper;
import com.tianitan.gen.service.UserService;
import com.tianitan.gen.model.entity.User;
import com.tianitan.gen.common.ResponseResult;
import com.tianitan.gen.common.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 用户信息存储表(User表)服务实现类
 *
 * @author tiantian
 * @since 2023-09-24 15:46:32
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param uid 主键
     * @return 实例对象
     */
    @Override
    public ResponseResult<User> selectById(Integer uid) {
        return ResponseResult.success(userMapper.selectById(uid));
    }

    /**
     * 分页查询
     *
     * @param pageNum  查询起始位置
     * @param pageSize 查询条数
     * @return 对象列表
     */
    @Override
    public ResponseResult<PageResult<User>> selectByPage(Integer pageNum, Integer pageSize, User user) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectList(user);

        PageInfo<User> pageInfo = new PageInfo<>(userList);

        List<User> result = pageInfo.getList();
        long total = pageInfo.getTotal();
        return ResponseResult.success(new PageResult<>(result, total));
    }

    /**
     * 根据条件查询
     *
     * @return 实例对象的集合
     */
    @Override
    public ResponseResult<List<User>> selectList(User user) {
        return ResponseResult.success(userMapper.selectList(user));
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseResult<Boolean> insert(User user) {
        return ResponseResult.success(userMapper.insert(user));
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseResult<Boolean> updateById(User user) {
        return ResponseResult.success(userMapper.updateById(user));
    }

    /**
     * 通过主键删除数据
     *
     * @param uid 主键
     * @return 是否成功
     */
    @Override
    public ResponseResult<Boolean> deleteById(Integer uid) {
        return ResponseResult.success(userMapper.deleteById(uid));
    }

}


